package com.zhangtai.modules.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangtai.common.annotation.SysAuthorityLog;
import com.zhangtai.modules.dao.EmployerCompanyDao;
import com.zhangtai.modules.entity.employee.EmployerCompanyEntity;
import com.zhangtai.modules.service.employee.EmployerCompanyService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("employerCompany")
@RestController
@Api(tags="甲方主体接口")
public class EmployerCompanyController {
    @Autowired
    private EmployerCompanyDao employerCompanyDao;
    @Autowired
    private EmployerCompanyService employerCompanyService;

    @GetMapping(value = "/findEmployerCompanyEntityList")
    @ApiOperation(value="获取甲方单位列表")
    public R<List<EmployerCompanyEntity>> findEmployerCompanyEntityList(){
        List<EmployerCompanyEntity> companyEntities = employerCompanyDao.selectList(new QueryWrapper<EmployerCompanyEntity>().eq("is_delete", 0));
        return R.ok(companyEntities);
    }


    @GetMapping("/queryEmployerCompanyEntityList")
    @ApiOperation(value = "分页查询薪甲方单位")
    public R<PageResult<EmployerCompanyEntity>> queryEmployerCompanyEntityList(@RequestParam(value = "name", required = false) String name,
                                                                               @RequestParam(value = "representative", required = false) String representative,
                                                                               @RequestParam(value = "registeredAddress", required = false) String registeredAddress,
                                                                               @RequestParam(value = "property", required = false) String property,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        PageResult<EmployerCompanyEntity> pageResult = employerCompanyService.queryPage(name,representative,registeredAddress,property, pageNum, pageSize);

        return R.ok(pageResult);

    }

    @GetMapping("/findEmployerCompanyById/{id}")
    @ApiOperation(value = "根据id查询薪甲方单位")
    public R<EmployerCompanyEntity> findEmployerCompanyById(@PathVariable("id") Long id){
        EmployerCompanyEntity byIdE = employerCompanyService.getByIdE(id);
        return R.ok(byIdE);
    }


    @DeleteMapping("/deleteEmployerCompanyById/{id}")
    @ApiOperation(value = "根据id查删除甲方单位")
    @SysAuthorityLog(value = "根据id查删除甲方单位")
    @Transactional
//    @PreAuthorize("hasAuthority('emp:contract:update')")
    public R deleteEmployerCompanyById(@PathVariable("id") Long id){
        R r = employerCompanyService.removeOneById(id);
        return r;
    }
    @PostMapping("/saveAndUpdateEmployerCompany")
    @ApiOperation(value = "薪资和修改甲方单位")
    @SysAuthorityLog(value = "薪资和修改甲方单位")
    @Transactional
//    @PreAuthorize("hasAuthority('emp:contract:update')")
    public R updateEmployerCompanyById(@RequestBody EmployerCompanyEntity employerCompanyEntity){
        R r;
        if(employerCompanyEntity.getId()==null){
          r=  employerCompanyService.saveE(employerCompanyEntity);
        }else {
           r = employerCompanyService.updateByIdE(employerCompanyEntity);
        }
        return r;
    }
}
